import module.StaticValue;
import view.CombatView;
import view.MenuView;
import view.View;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

public class MainApp extends JFrame {
    /**
     *  存储视图的数组
     */
    private List<View> vl;
    public MainApp(){
        vl=new ArrayList<View>();
        //设置主程序窗口大小
        this.setSize(StaticValue.windowWidth,StaticValue.windowHeight);
        //设置窗体居中
        this.setLocationRelativeTo(null);
        //设置可见性
        this.setVisible(true);
        //设置默认关闭窗体按钮
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置大小不可变
        this.setResizable(false);
        //设置窗体标题
        this.setTitle(StaticValue.windowTitle);
    }

    /**
     * 向主程序(主窗口)添加视图
     * @param v
     */
    public void addView(View v){
        vl.add(v);
        this.add(v);
    }

    /**
     * 设置当前视图为index索引位置.
     * 对应索引位置视图激活，其他索引位置视图隐藏
     * @param index 视图索引，与视图的添加顺序有关
     */
    public void setShowedView(int index){
        View view = vl.get(index);
        view.setVisible(true);
        view.requestFocus();
        vl.forEach(v->{
            if(!v.equals(view)){
                v.setVisible(false);
            }
        });
    }

    public static void main(String[] args) {
        MainApp mainApp = new MainApp();
        MenuView mv = new MenuView();
        CombatView cv = new CombatView();
        mv.addListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                mainApp.setShowedView(1);
            }
        });
        mainApp.addView(mv);
        mainApp.addView(cv);
        mainApp.setShowedView(0);
    }
}
